package com.darren.befriend.fragments;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.darren.befriend.R;
import com.darren.befriend.adapters.InterestAdapter;
import com.darren.befriend.models.HobbyModel;

public class HobiesPageFragment extends Fragment {

	public static final String ARG_PAGE = "page";
	private int mPageNumber;
	private GridView mGridview;
	private ArrayList<HobbyModel> mInterestArr;
	private ArrayList<HobbyModel> mSporttArr;
	private ArrayList<HobbyModel> mMusicArr;
	private InterestAdapter mInterestAdapter;

	private Integer[] mInterestResIds = { R.drawable.int33, R.drawable.int34,
			R.drawable.int35, R.drawable.int36, R.drawable.int37,
			R.drawable.int38, R.drawable.int39 };

	private Integer[] mSportResIds = { R.drawable.int33, R.drawable.int39,
			R.drawable.int34, R.drawable.int35, R.drawable.int36,
			R.drawable.int37, R.drawable.int38, R.drawable.int39,
			R.drawable.int35 };

	private Integer[] mMusicResIds = { R.drawable.int36, R.drawable.int37,
			R.drawable.int33, R.drawable.int34, R.drawable.int35,
			R.drawable.int38, R.drawable.int39 };

	/**
	 * create Fragment in ViewPager
	 * 
	 * @param pageNumber
	 * @return
	 */
	public static Fragment create(int pageNumber) {
		HobiesPageFragment fragment = new HobiesPageFragment();
		Bundle args = new Bundle();
		args.putInt(ARG_PAGE, pageNumber);
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mPageNumber = getArguments().getInt(ARG_PAGE);
	}

	private ArrayList<HobbyModel> createHobbiesArr(String[] hobbiesTextArr,
			Integer[] hobbiesResIds) {
		if (hobbiesTextArr.length == hobbiesResIds.length) {
			ArrayList<HobbyModel> hobbiesArr = new ArrayList<HobbyModel>();
			for (int i = 0; i < hobbiesTextArr.length; i++) {
				HobbyModel interestItem = new HobbyModel();
				interestItem.interestIconRes = hobbiesResIds[i];
				interestItem.interestText = hobbiesTextArr[i];
				hobbiesArr.add(interestItem);
			}
			return hobbiesArr;
		}
		return null;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		ViewGroup rootView = (ViewGroup) inflater.inflate(
				R.layout.layout_interest_pager, container, false);
		mGridview = (GridView) rootView.findViewById(R.id.hobies_gridview);
		String[] interestTextArr;
		String[] musicTextArr;
		String[] sportTextArr;

		if (mPageNumber == 0) {
			interestTextArr = getResources().getStringArray(
					R.array.interest_text_arr);
			mInterestArr = createHobbiesArr(interestTextArr, mInterestResIds);
			mInterestAdapter = new InterestAdapter(getActivity(), mInterestArr);
		} else if (mPageNumber == 1) {
			sportTextArr = getResources()
					.getStringArray(R.array.sport_text_arr);
			mSporttArr = createHobbiesArr(sportTextArr, mSportResIds);
			mInterestAdapter = new InterestAdapter(getActivity(), mSporttArr);

		} else if (mPageNumber == 2) {
			musicTextArr = getResources()
					.getStringArray(R.array.music_text_arr);
			mMusicArr = createHobbiesArr(musicTextArr, mMusicResIds);
			mInterestAdapter = new InterestAdapter(getActivity(), mMusicArr);
		}
		mGridview.setNumColumns(3);
		mGridview.setAdapter(mInterestAdapter);
		mGridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				if (mPageNumber == 0) {
					mInterestArr.get(position).isSelected = !mInterestArr
							.get(position).isSelected;
				} else if (mPageNumber == 1) {
					mSporttArr.get(position).isSelected = !mSporttArr
							.get(position).isSelected;

				} else if (mPageNumber == 2) {
					mMusicArr.get(position).isSelected = !mMusicArr
							.get(position).isSelected;
				}

				mInterestAdapter.notifyDataSetChanged();
			}
		});
		return rootView;
	}

}
